package com.example.demo.mapper;

import com.example.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.StringUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Description
 */
@Slf4j
public class UserDaoImpl implements UserDao {

//    private static ApplicationContext applicationContext;
//
//    public static void setApplicationContext(ApplicationContext applicationContext) {
//        UserDaoImpl.applicationContext = applicationContext;
//    }

    @Override
    public List<User> findUserList(User user) throws SQLException {
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai",
                "root", "153687");
        Statement statement = connection.createStatement();
//        JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
        StringBuilder sql = new StringBuilder("SELECT `id`,`username`,`age`,`password` FROM `user` WHERE 1=1");
        Map<String, Object> map = new HashMap<>();
        if (user.getId() != null && user.getId() > 0) {
            sql.append(" AND `id` = ");
            sql.append(user.getId());
//            sql.append(" AND `id` = :id");
            map.put("id", user.getId());
        }
        if (StringUtils.hasText(user.getUsername())) {
            sql.append(" AND `username` = :username");
            map.put("username", user.getUsername());
        }
        if (user.getAge() != null && user.getAge() > 0) {
            sql.append(" AND `age` = :age");
            map.put("age", user.getAge());
        }
        if (StringUtils.hasText(user.getPassword())) {
            sql.append(" AND `password` = :password");
            map.put("password", user.getPassword());
        }
//        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(jdbcTemplate);
        ResultSet resultSet = statement.executeQuery(String.valueOf(sql));

        User u;
        List<User> userList = new ArrayList<>();
        int i = 0;
        while (resultSet.next()) {
            i++;
            System.out.println(i);
            int id = resultSet.getInt("id");
            String name = resultSet.getString("username");
            System.out.println("id:" + id + ",username:" + name);
            u = new User();
            u.setId(resultSet.getInt("id"));
            u.setAge(resultSet.getInt("age"));
            u.setUsername(resultSet.getString("username"));
            u.setPassword(resultSet.getString("password"));
            userList.add(u);
        }
        //
//        return jdbc.queryForList(sql.toString(), map, User.class);
        resultSet.close();
        statement.close();
        connection.close();
        return userList;
    }
}
